/*
 * Developed by Fatih Girisken and Jan Brennenstuhl,
 * Graspdroid is an educational, figurative game for kids,
 * training not just to handle Android devices but also
 * to comprehend and understand geometrically forms.
 * Graspdroid, Potsdam University
 */


package de.berlinpiraten.graspdroid;

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.Random;
import java.util.Timer;
import java.util.TimerTask;

import javax.microedition.khronos.opengles.GL10;

import org.anddev.andengine.engine.Engine;
import org.anddev.andengine.engine.camera.Camera;
import org.anddev.andengine.engine.options.EngineOptions;
import org.anddev.andengine.engine.options.EngineOptions.ScreenOrientation;
import org.anddev.andengine.engine.options.resolutionpolicy.RatioResolutionPolicy;
import org.anddev.andengine.entity.scene.Scene;
import org.anddev.andengine.entity.scene.background.SpriteBackground;
import org.anddev.andengine.entity.sprite.Sprite;
import org.anddev.andengine.entity.text.ChangeableText;
import org.anddev.andengine.entity.text.Text;
import org.anddev.andengine.entity.util.FPSLogger;
import org.anddev.andengine.input.touch.TouchEvent;
import org.anddev.andengine.opengl.font.Font;
import org.anddev.andengine.opengl.font.StrokeFont;
import org.anddev.andengine.opengl.texture.Texture;
import org.anddev.andengine.opengl.texture.TextureOptions;
import org.anddev.andengine.opengl.texture.region.TextureRegion;
import org.anddev.andengine.opengl.texture.region.TextureRegionFactory;
import org.anddev.andengine.opengl.util.GLHelper;
import org.anddev.andengine.ui.activity.BaseGameActivity;
import org.anddev.andengine.util.HorizontalAlign;

import android.graphics.Color;
import android.graphics.Typeface;
import android.view.MotionEvent;

public class GraspdroidActivity extends BaseGameActivity {
	// ===========================================================
	// Constants
	// ===========================================================

	private static final int CAMERA_WIDTH = 800;
	private static final int CAMERA_HEIGHT = 480;

	//private static final int DARK_GREY = Color.rgb(160, 160, 160);
	private static final int WHITE = Color.rgb(255, 255, 255);

	private static final int GEO_GFX = 9;
	
	private static final int[][] GRID = {
		{30,30},{30,180},{30,330},
		{197,30},{197,180},{197,330}, 
		{363,30},{363,180},{363,330}, 
	};
	
	//time of the one short play
	 private static final int p_seconds = 5;
	//time of the one level (the game at the moment)
	 private static final int g_seconds = 20;
	

	// ===========================================================
	// Fields
	// ===========================================================
	
	// Basics
	private Camera mCamera;
	private Texture mInfosStrokeFontTexture;
	private Font mInfosStrokeFont;
	
	// Texts
	private ChangeableText mPlayTimeInfoText;
	private ChangeableText mTriesInfoText;
	private ChangeableText mLevelTimeInfoText;
	private ChangeableText mScoreInfoText;
	
	// Scenes
    private Scene mStartScene;
    private Scene mGameScene;
    private Scene mAboutScene;
    private Scene mEndOfLevelScene;
    
    // Geo Textures
	private List<Texture> mObjectsTexture = new ArrayList<Texture>();
	private List<TextureRegion> mObjectsTextureRegion = new ArrayList<TextureRegion>();
	
	// Start Textures
	private Texture mStartButtonTexture;
    private TextureRegion mStartButtonTextureRegion;
    private Sprite mStartButtonSprite;
    
    private Texture mAboutButtonTexture;
    private Texture mAboutBackButtonTexture;
    private TextureRegion mAboutButtonTextureRegion;
    private TextureRegion mAboutBackButtonTextureRegion;
    private Sprite mAboutButtonSprite;
    private Sprite mAboutBackButtonSprite;
    
    private Texture mPlayAgainTexture;
    private TextureRegion mPlayAgainTextureRegion;
    private Sprite mPlayAgainSprite;
    
    private Texture mStartBackgroundTexture;
    private TextureRegion mStartBackgroundTextureRegion;
    private Sprite mStartBackgroundSprite;
    
    // Game Texture
 	private Texture mGameBackgroundTexture;
    private TextureRegion mGameBackgroundTextureRegion;
    private Sprite mGameBackgroundSprite;
    
    // About Texture
  	private Texture mAboutBackgroundTexture;
    private TextureRegion mAboutBackgroundTextureRegion;
    private Sprite mAboutBackgroundSprite;
    
    // Game variables
    private int targetId;
    private Sprite mTargetSprite;
    private List<Sprite> mListSprite = new ArrayList<Sprite>();
    private Timer timer = new Timer();
    private int score = 0;
    private int corrects = 0;
    private int wrongs = 0;
    private int timeOuts = 0;
    private boolean correctFlag = false;
    private int levelTimeCounter = 0; 
	private int playTimeCounter = 0; 
	private String difficulty = "Default";
    
    

	// ===========================================================
	// Methods for/from SuperClass/Interfaces
	// ===========================================================

	public Engine onLoadEngine() {
		// Camera - Defines the rectangle of the scene that is drawn on the screen.
		// Engine - Makes the game proceed in small discrete steps of time.
		this.mCamera = new Camera(0, 0, CAMERA_WIDTH, CAMERA_HEIGHT);

		RatioResolutionPolicy reso = new RatioResolutionPolicy(CAMERA_WIDTH, CAMERA_HEIGHT);
		EngineOptions opt = new EngineOptions(true, ScreenOrientation.LANDSCAPE, reso, this.mCamera);

		return new Engine(opt);
	}

	public void onLoadResources() {
		loadFont();
		loadGameGfx();
		loadMiscGfx();
	}

	public Scene onLoadScene() {
		this.mEngine.registerUpdateHandler(new FPSLogger());
        
    	createStartScene();
    	createGameScene();
    	createAboutScene();
    	createEndOfLevelScene();
        
        return mStartScene;
	}

	public void onLoadComplete() {
		// TODO Auto-generated method stub

	}
	 
	// ===========================================================
	// Methods
	// ===========================================================
	 
	private void loadFont(){
		this.mInfosStrokeFontTexture = new Texture(512, 512, TextureOptions.BILINEAR);
		this.mInfosStrokeFont = new StrokeFont(
				this.mInfosStrokeFontTexture, Typeface.createFromAsset(this.getAssets(), "font/Kabel.ttf"), 18, true, WHITE, 0, WHITE);
		this.mEngine.getTextureManager().loadTexture(mInfosStrokeFontTexture);
		this.mEngine.getFontManager().loadFonts(this.mInfosStrokeFont);
	}
	
	private void loadGameGfx(){
		for (int i = 1; i <= GEO_GFX; i++) {
			Texture MiniTexture = new Texture(128, 128, TextureOptions.BILINEAR_PREMULTIPLYALPHA);
			TextureRegion miniTextureRegion = TextureRegionFactory.createFromAsset(MiniTexture, this,"gfx/geoForms/" + String.valueOf(i) + ".png", 0, 0);

			this.mObjectsTexture.add(MiniTexture);
			this.mObjectsTextureRegion.add(miniTextureRegion);
			this.mEngine.getTextureManager().loadTexture(
			this.mObjectsTexture.get(i - 1));
		}
	}
	
	private void loadMiscGfx(){
		this.mStartButtonTexture = new Texture(256, 64, TextureOptions.BILINEAR_PREMULTIPLYALPHA);
		this.mStartButtonTextureRegion = TextureRegionFactory.createFromAsset(this.mStartButtonTexture, this, "gfx/start.png", 0, 0);
		this.mEngine.getTextureManager().loadTexture(mStartButtonTexture);
		
		this.mPlayAgainTexture = new Texture(256, 64, TextureOptions.BILINEAR_PREMULTIPLYALPHA);
		this.mPlayAgainTextureRegion = TextureRegionFactory.createFromAsset(this.mPlayAgainTexture, this, "gfx/playAgain.png", 0, 0);
		this.mEngine.getTextureManager().loadTexture(mPlayAgainTexture);
		
		this.mAboutBackButtonTexture = new Texture(256, 64, TextureOptions.BILINEAR_PREMULTIPLYALPHA);
		this.mAboutBackButtonTextureRegion = TextureRegionFactory.createFromAsset(this.mAboutBackButtonTexture, this, "gfx/aboutBack.png", 0, 0);
		this.mEngine.getTextureManager().loadTexture(mAboutBackButtonTexture);
		
		this.mAboutButtonTexture = new Texture(256, 64, TextureOptions.BILINEAR_PREMULTIPLYALPHA);
		this.mAboutButtonTextureRegion = TextureRegionFactory.createFromAsset(this.mAboutButtonTexture, this, "gfx/about.png", 0, 0);
		this.mEngine.getTextureManager().loadTexture(mAboutButtonTexture);
		
		this.mStartBackgroundTexture = new Texture(1024, 512, TextureOptions.BILINEAR_PREMULTIPLYALPHA);
		this.mStartBackgroundTextureRegion = TextureRegionFactory.createFromAsset(this.mStartBackgroundTexture, this, "gfx/start_bg.png", 0, 0);
		this.mEngine.getTextureManager().loadTexture(mStartBackgroundTexture);
		
		this.mGameBackgroundTexture = new Texture(1024, 512, TextureOptions.BILINEAR_PREMULTIPLYALPHA);
		this.mGameBackgroundTextureRegion = TextureRegionFactory.createFromAsset(this.mGameBackgroundTexture, this, "gfx/game_bg.png", 0, 0);
		this.mEngine.getTextureManager().loadTexture(mGameBackgroundTexture);
		
		this.mAboutBackgroundTexture = new Texture(1024, 512, TextureOptions.BILINEAR_PREMULTIPLYALPHA);
		this.mAboutBackgroundTextureRegion = TextureRegionFactory.createFromAsset(this.mAboutBackgroundTexture, this, "gfx/about_bg.png", 0, 0);
		this.mEngine.getTextureManager().loadTexture(mAboutBackgroundTexture);
	}
	
	private void createStartScene(){
		mStartScene = new Scene(1);
		
        mStartScene.setBackgroundEnabled(true);
        mStartScene.setBackground(loadBackground(mStartBackgroundSprite, mStartBackgroundTextureRegion));
        
        // Start Button
        mStartButtonSprite = new Sprite(120, 200, mStartButtonTextureRegion){
        	protected void onInitDraw(final GL10 pGL)
            {
               super.onInitDraw(pGL);
               GLHelper.enableTextures(pGL);
               GLHelper.enableTexCoordArray(pGL);
               GLHelper.enableDither(pGL);
            }
        	
        	@Override
        	public boolean onAreaTouched(final TouchEvent pSceneTouchEvent, final float pTouchAreaLocalX, final float pTouchAreaLocalY) {
        		if(pSceneTouchEvent.getAction() == MotionEvent.ACTION_DOWN){
        			mStartScene.setChildScene(mGameScene, false, true, true);	
        			updateTimerText(); //starts the timer for a new game. It starts just after the Gamescene is set as a child scene so the counter starts with the scene syncronised.
        		}
        		return true;
        	}
        };
        mStartButtonSprite.setBlendFunction(GL10.GL_ONE, GL10.GL_ONE_MINUS_SRC_ALPHA);
        mStartButtonSprite.setVisible(true);
        
        mStartScene.attachChild(mStartButtonSprite);
        mStartScene.registerTouchArea(mStartButtonSprite);
        
        // About Button
        mAboutButtonSprite = new Sprite(420, 200, mAboutButtonTextureRegion){
        	protected void onInitDraw(final GL10 pGL)
            {
               super.onInitDraw(pGL);
               GLHelper.enableTextures(pGL);
               GLHelper.enableTexCoordArray(pGL);
               GLHelper.enableDither(pGL);
            }
        	
        	@Override
        	public boolean onAreaTouched(final TouchEvent pSceneTouchEvent, final float pTouchAreaLocalX, final float pTouchAreaLocalY) {
        		if(pSceneTouchEvent.getAction() == MotionEvent.ACTION_DOWN){
        			mStartScene.setChildScene(mAboutScene, false, true, true);	
        		}
        		return true;
        	}
        };
        mAboutButtonSprite.setBlendFunction(GL10.GL_ONE, GL10.GL_ONE_MINUS_SRC_ALPHA);
        mAboutButtonSprite.setVisible(true);
        
        mStartScene.attachChild(mAboutButtonSprite);
        mStartScene.registerTouchArea(mAboutButtonSprite);
	}
	
	private void createAboutScene(){
		mAboutScene = new Scene(1);
        
		mAboutScene.setBackgroundEnabled(true);
		mAboutScene.setBackground(loadBackground(mAboutBackgroundSprite, mAboutBackgroundTextureRegion));
        
    	final Text aboutText = new Text(150, 120, this.mInfosStrokeFont, "Developed by Fatih Girisken and Jan Brennenstuhl,\nGraspdroid is an educational, figurative game for kids,\ntraining not just to handle Android devices but also\nto comprehend and understand geometrically forms.\n\n Graspdroid, Potsdam University", HorizontalAlign.LEFT);
    	aboutText.setBlendFunction(GL10.GL_SRC_ALPHA, GL10.GL_ONE_MINUS_SRC_ALPHA);
    	aboutText.setVisible(true);
    	
    	// About back Button. sets the scene back to the startscene.
        mAboutBackButtonSprite = new Sprite(140, 290, mAboutBackButtonTextureRegion){
        	protected void onInitDraw(final GL10 pGL)
            {
               super.onInitDraw(pGL);
               GLHelper.enableTextures(pGL);
               GLHelper.enableTexCoordArray(pGL);
               GLHelper.enableDither(pGL);
            }
        	
        	@Override
        	public boolean onAreaTouched(final TouchEvent pSceneTouchEvent, final float pTouchAreaLocalX, final float pTouchAreaLocalY) {
        		if(pSceneTouchEvent.getAction() == MotionEvent.ACTION_DOWN){
        			createStartScene();
        			mEngine.setScene(mStartScene);
        		}
        		return true;
        	}
        };
        mAboutBackButtonSprite.setBlendFunction(GL10.GL_ONE, GL10.GL_ONE_MINUS_SRC_ALPHA);
        mAboutBackButtonSprite.setVisible(true);
		
    	mAboutScene.getLastChild().attachChild(aboutText);
    	mAboutScene.attachChild(mAboutBackButtonSprite);
    	mAboutScene.registerTouchArea(mAboutBackButtonSprite);
  
    }
	
	//creates the scene to be shown at the end of the level, showing all the information about the level.
	private void createEndOfLevelScene(){
		mEndOfLevelScene = new Scene(1);
        
		mEndOfLevelScene.setBackgroundEnabled(true);
		mEndOfLevelScene.setBackground(loadBackground(mAboutBackgroundSprite, mAboutBackgroundTextureRegion));
        
		final Text aboutText = new Text(150, 120, this.mInfosStrokeFont, "Time is up. End of the Level\nDifficulty: " + difficulty + 
				"\nScore: " + score + 
				"\nNumber of Corrects: " + corrects + " X 10 = " + calCorrectScore() +
				"\nNumber of Wrongs: " + wrongs + " X 2 = " + calWrongScore() +
				"\nNumber of time outs: " + timeOuts + " X 5 = " + calTimeOutScore()+
				"\n\nTotal Score: " + calTotalScore() +
				"\n\nDeveloped by Fatih Girisken and Jan Brennenstuhl" +
				"\nThank you for playing."
				, HorizontalAlign.LEFT);
		aboutText.setBlendFunction(GL10.GL_SRC_ALPHA, GL10.GL_ONE_MINUS_SRC_ALPHA);
    	aboutText.setVisible(true);
    	
    	// Play again Button. resets all the game values of the level and sets the current scene to the startscene
        mPlayAgainSprite = new Sprite(500, 110, mPlayAgainTextureRegion){
        	protected void onInitDraw(final GL10 pGL)
            {
               super.onInitDraw(pGL);
               GLHelper.enableTextures(pGL);
               GLHelper.enableTexCoordArray(pGL);
               GLHelper.enableDither(pGL);
            }
        	
        	@Override
        	public boolean onAreaTouched(final TouchEvent pSceneTouchEvent, final float pTouchAreaLocalX, final float pTouchAreaLocalY) {
        		if(pSceneTouchEvent.getAction() == MotionEvent.ACTION_DOWN){
        			startNewGame();
        			createStartScene();
        			mEngine.setScene(mStartScene);
        			
        		}
        		return true;
        	}
        };
        mPlayAgainSprite.setBlendFunction(GL10.GL_ONE, GL10.GL_ONE_MINUS_SRC_ALPHA);
        mPlayAgainSprite.setVisible(true);
		
    	mEndOfLevelScene.getLastChild().attachChild(aboutText);
    	mEndOfLevelScene.attachChild(mPlayAgainSprite);
    	mEndOfLevelScene.registerTouchArea(mPlayAgainSprite);
    	
    }
	
	private void createGameScene(){
    	mGameScene = new Scene(1);
        
    	mGameScene.setBackgroundEnabled(true);
    	mGameScene.setBackground(loadBackground(mGameBackgroundSprite, mGameBackgroundTextureRegion));
        
    	final Text introText = new Text(565, 75, this.mInfosStrokeFont, "Can you find this figure?\nHurry up, time is running!", HorizontalAlign.LEFT);
    	introText.setBlendFunction(GL10.GL_SRC_ALPHA, GL10.GL_ONE_MINUS_SRC_ALPHA);
    	introText.setVisible(true);
    	
    	mLevelTimeInfoText = new ChangeableText(565, 40, this.mInfosStrokeFont, "Time of the level: 0", "Time of the level: XXs".length());
    	mLevelTimeInfoText.setBlendFunction(GL10.GL_SRC_ALPHA, GL10.GL_ONE_MINUS_SRC_ALPHA);
    	mLevelTimeInfoText.setAlpha(1.f);
    	mLevelTimeInfoText.setVisible(true);
    	
        mPlayTimeInfoText = new ChangeableText(565, 310, this.mInfosStrokeFont, "Time: 0", "Time: XXs".length());
        mPlayTimeInfoText.setBlendFunction(GL10.GL_SRC_ALPHA, GL10.GL_ONE_MINUS_SRC_ALPHA);
        mPlayTimeInfoText.setAlpha(1.f);
        mPlayTimeInfoText.setVisible(true);
        
        mTriesInfoText = new ChangeableText(677, 310, this.mInfosStrokeFont, "Wrongs: 0", "Tries: XXXX".length());
        mTriesInfoText.setBlendFunction(GL10.GL_SRC_ALPHA, GL10.GL_ONE_MINUS_SRC_ALPHA);
        mTriesInfoText.setAlpha(1.f);
        mTriesInfoText.setVisible(true);
        
        mScoreInfoText = new ChangeableText(620, 345, this.mInfosStrokeFont, "Score: 0", "Score: XXX".length()+1);
    	mScoreInfoText.setBlendFunction(GL10.GL_SRC_ALPHA, GL10.GL_ONE_MINUS_SRC_ALPHA);
    	mScoreInfoText.setAlpha(1.f);
    	mScoreInfoText.setVisible(true);
		
		mGameScene.getLastChild().attachChild(introText);
		mGameScene.getLastChild().attachChild(mPlayTimeInfoText);
		mGameScene.getLastChild().attachChild(mTriesInfoText);
		mGameScene.getLastChild().attachChild(mLevelTimeInfoText);
		mGameScene.getLastChild().attachChild(mScoreInfoText);
		
		for (int i=0; i<GEO_GFX; i++){        	
    		Sprite geoSprite = new GeoButton(GRID[i][0], GRID[i][1], mObjectsTextureRegion.get(i), i){
        		
        		protected void onInitDraw(final GL10 pGL)
        		{
		            super.onInitDraw(pGL);
		            GLHelper.enableTextures(pGL);
		            GLHelper.enableTexCoordArray(pGL);
		            GLHelper.enableDither(pGL);
        		}
        		
        		@Override
            	public boolean onAreaTouched(final TouchEvent pSceneTouchEvent, final float pTouchAreaLocalX, final float pTouchAreaLocalY) {
        			if(pSceneTouchEvent.getAction() == MotionEvent.ACTION_MOVE){
        				this.setAlpha(1.f);
        			}
        			if(pSceneTouchEvent.getAction() == MotionEvent.ACTION_DOWN){
            			this.setAlpha(0.6f);
            		}
            		if(pSceneTouchEvent.getAction() == MotionEvent.ACTION_DOWN){
            			this.setAlpha(1.f);
            			if (CheckCorrectObject(objectId)){
        					reloadTextures();
        					writeScore(10);  //write 10 points for the correct answer.
        				}
            		}
            		return true;
            	}
        	};
        	geoSprite.setBlendFunction(GL10.GL_ONE, GL10.GL_ONE_MINUS_SRC_ALPHA);
        	geoSprite.setAlpha(1.f);
        	this.mGameScene.getLastChild().attachChild(geoSprite);
            this.mGameScene.registerTouchArea(geoSprite);
            
            geoSprite.setVisible(true);
            
            mListSprite.add(geoSprite);
        }    
        
        loadRandomTarget();

    }
	
	//reloading textures when user plays correct or when the time's up
	private void reloadTextures(){
		resetConfiguration();
		loadRandomTarget();
		ShuffleGrid();
	}
	//all the variables in the following couple of methods are globally declared to be used in the whole game. 
	//adds the score to the total and writes it on the screen
	private void writeScore(int s){
		this.score = this.score + s;
		mScoreInfoText.setText("Score: " + this.score);
	}
	
	//calculates the wrong answers. 2 points subtracted for each.
	private int calWrongScore(){
		return wrongs*(-2);
	}
	
	//calculates the score for correct answers. 10 points for each.
	private int calCorrectScore(){
		return corrects*10;
	}
	
	//calculates the value to be subtracted from the total score. 5 points for each time out cuts.
	private int calTimeOutScore(){
		return timeOuts*(-5);
	}
	
	//calculates the total score. is called at the end of the level.
	private int calTotalScore(){
		return calCorrectScore()+calWrongScore()+calTimeOutScore();
	}
	
	//resetting all game values for a new game
	private void startNewGame(){
		score = 0;
		this.wrongs = 0;
		corrects = 0;
		timeOuts = 0;
		playTimeCounter = 0;
		levelTimeCounter = 0;
		correctFlag = false;
		timer.cancel();
		writeScore(0);
	}
	
	//updates the text and the variables each second; behaves as time counter. It is called first when the gamescene is attached. 
	private void updateTimerText(){
		
		
		timer = new Timer();
		
	       
	        timer.schedule(new TimerTask() {
				
				@Override
				public void run() {
					if( levelTimeCounter <= g_seconds){
						mLevelTimeInfoText.setText("Time of the level: " + levelTimeCounter + "s" );
						mPlayTimeInfoText.setText("Time: " + playTimeCounter + "s");
						
						 //when the time is up, the textures will reload and player will lose points
						if ( (playTimeCounter == p_seconds) & (levelTimeCounter != 0) & (!correctFlag)){
							writeScore(-5);
							reloadTextures();	
							timeOuts++;
							playTimeCounter = 0;
						}
						
						levelTimeCounter++;
						playTimeCounter++;
					}
					//when the time of the level comes to an end.
					if ( levelTimeCounter == g_seconds+1 ) {
						levelTimeCounter = 0;
				    	createEndOfLevelScene();
						mEngine.setScene(mEndOfLevelScene);
						
					}
					
				}
			}, 0, 1000); //1000 msec
	        
	        
	        
	}
	
	private void loadRandomTarget(){
    	Random r = new Random();
    	
    	targetId = r.nextInt(GEO_GFX);
        mTargetSprite = new Sprite(600, 150, mObjectsTextureRegion.get(targetId)){
        	protected void onInitDraw(final GL10 pGL)
            {
               super.onInitDraw(pGL);
               GLHelper.enableTextures(pGL);
               GLHelper.enableTexCoordArray(pGL);
               GLHelper.enableDither(pGL);
            }
        };
        mTargetSprite.setBlendFunction(GL10.GL_ONE, GL10.GL_ONE_MINUS_SRC_ALPHA);        
        mTargetSprite.setAlpha(1.f);
        this.mGameScene.getLastChild().attachChild(mTargetSprite);
        mTargetSprite.setVisible(true);
    }
	
	private SpriteBackground loadBackground(Sprite background, TextureRegion region){
		background = new Sprite(0, 0, region){
        	protected void onInitDraw(final GL10 pGL)
            {
               super.onInitDraw(pGL);
               GLHelper.enableTextures(pGL);
               GLHelper.enableTexCoordArray(pGL);
               GLHelper.enableDither(pGL);
            }
        };
        background.setBlendFunction(GL10.GL_ONE, GL10.GL_ONE_MINUS_SRC_ALPHA);
        background.setVisible(true);
        
        return new SpriteBackground(background);
	}
	
    private Boolean CheckCorrectObject(int id){
    	if (id==targetId){
       		mTriesInfoText.setText("Wrongs: " + wrongs);
    		corrects++;
    		correctFlag = true;
    		playTimeCounter = 0;
    		return true;
    	} else{
    		wrongs++;
    		mTriesInfoText.setText("Wrongs: " + wrongs);
    		correctFlag = false;
    		return false;
    	}
    }
    
  //Creates an arraylist of shuffled integers in the number of Geo graphics
    private ArrayList<Integer> generateRandomNumbers(){
    	
 	  ArrayList<Integer> numbers = new ArrayList<Integer>();
 	    
 	  for (int i=0; i<GEO_GFX; i++){  
 		  numbers.add(i);
 	  }
 	  Collections.shuffle(numbers);
    	return numbers;
    }
    
    //changes the positions of each sprite in the list using the generateRandomNumbers()
    //This method is called inside the timer to shuffle the object in the grid according to the rules of the game.
    private void ShuffleGrid(){
    	
    	ArrayList<Integer> numbers = new ArrayList<Integer>();
    	numbers = generateRandomNumbers();
 
		for (int i=0; i<GEO_GFX; i++){        	
				
		 	 mListSprite.get(i).setPosition(GRID[numbers.get(i)][0], GRID[numbers.get(i)][1]);
		 	
		}
		  
    }
    
    
    private void resetConfiguration(){
    	mTargetSprite.setVisible(false);
    }
}